package com.zichen.pr.demo.student.controller;

import com.zichen.pr.common.constant.UserConstants;
import com.zichen.pr.common.core.controller.BaseController;
import com.zichen.pr.common.core.domain.result.Result;
import com.zichen.pr.common.core.page.TableDataInfo;
import com.zichen.pr.demo.student.entity.Student;
import com.zichen.pr.demo.student.service.StudentService;
import com.zichen.pr.util.DateUtils;
import com.zichen.pr.util.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: zichen
 * @description: 测试学生控制层
 * @author: rzhang
 * @create: 2021-04-26 08:28
 */
@RestController
@RequestMapping("/cs/student")
public class StudentController extends BaseController {

    @Resource
    private StudentService studentService;

    /**
     * 获取学生测试列表
     */
    @PreAuthorize("@ss.hasPermi('cs:student:list')")
    @GetMapping("/list")
    public TableDataInfo list(Student student) {
        startPage();
        List<Student> list = studentService.selectStudentList(student);
        return getDataTable(list);
    }

    /**
     * 根据学生测试编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('cs:student:query')")
    @GetMapping(value = "/{u_id}")
    public Result getInfo(@PathVariable Long u_id) {
        return Result.success(studentService.getById(u_id));
    }

    /**
     * 新增学生测试
     */
    @PreAuthorize("@ss.hasPermi('cs:student:add')")
    @PostMapping
    public Result add(@Validated @RequestBody Student student) {
        if (UserConstants.NOT_UNIQUE.equals(studentService.checkNameUnique(student))) {
            return Result.error("新增学生测试'" + student.getU_name() + "'失败，学生测试名称已存在");
        }
        student.setCreateBy(SecurityUtils.getUsername());
        student.setCreateTime(DateUtils.getNowDate());
        return toAjax(studentService.save(student));
    }

    /**
     * 修改学生测试
     */
    @PreAuthorize("@ss.hasPermi('cs:student:edit')")
    @PutMapping
    public Result edit(@Validated @RequestBody Student student) {
        if (UserConstants.NOT_UNIQUE.equals(studentService.checkNameUnique(student))) {
            return Result.error("修改学生测试'" + student.getU_name() + "'失败，学生测试名称已存在");
        }
        student.setUpdateBy(SecurityUtils.getUsername());
        student.setUpdateTime(DateUtils.getNowDate());
        return toAjax(studentService.updateById(student));
    }

    /**
     * 删除学生测试
     */
    @PreAuthorize("@ss.hasPermi('cs:student:remove')")
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids) {
        return toAjax(studentService.deleteStudentByIds(ids));
    }
}
